//
//  AddAddressViewController.m
//  PeopleStreet
//
//  Created by 辰领科技 on 16/4/14.
//  Copyright © 2016年 yuf. All rights reserved.
//

#import "AddAddressViewController.h"
#import "ScroButton.h"

#import "DbDao.h"
#import "CityArea.h"
#import "ChoseAddressMapViewController.h"

@interface AddAddressViewController ()
@property (strong, nonatomic) IBOutlet UIView *navView;
@property (strong, nonatomic) IBOutlet UIScrollView *scroView;
@property (strong, nonatomic) IBOutlet UILabel *titleLabel;
@property (strong, nonatomic) IBOutlet UITextField *recivername;
@property (strong, nonatomic) IBOutlet UITextField *phoneField;
@property (strong, nonatomic) IBOutlet UITextField *addressSection;
@property (strong, nonatomic) IBOutlet UITextField *detailAddress;

@property (strong, nonatomic) IBOutlet UIButton * saveButton;
@property(nonatomic,strong)ScroButton * scroButton;
@property (strong, nonatomic) IBOutlet UIButton * cancleButton;


@property(nonatomic,strong)NSString * captionId;
@property(nonatomic,strong)NSString * cityId;
@property(nonatomic,strong)NSString * sectionId;
@property(nonatomic,strong)NSString * captionName;
@property(nonatomic,strong)NSString * cityName;
@property(nonatomic,strong)NSString * sectionName;
@end

@implementation AddAddressViewController

- (void)viewDidLoad {
    [super viewDidLoad];


    // Do any additional setup after loading the view.
    [self initViewLayout];
}

- (void)initViewLayout{
    [self adaptiveViewLayout:self.view];
    [self adaptiveViewLayout:self.navView];
    [self adaptiveViewLayout:self.scroView];
    
    _saveButton.layer.cornerRadius = 19*VerticalRatio();
    _scroView.contentSize = flexibleSize(CGSizeMake(0, 505), NO);
    
    _scroButton = [[ScroButton alloc]initWithFrame:flexibleFrame(CGRectMake(250, 205, 48, 22), NO) title:@"" selectedTitle:@"" selectedColor:[UIColor colorWithRed:1.000 green:0.467 blue:0.004 alpha:1.000] cancelColor:[UIColor colorWithWhite:0.851 alpha:1.000] selected:NO complect:^(NSInteger index) {
        
    }];
    [_scroView addSubview:_scroButton];
    
    
    if (_addressInfo) {//编辑
        [_cancleButton setTitle:@"删除" forState:UIControlStateNormal];
        _captionId =_addressInfo[@"msadProvinceId"];
        _captionName = _addressInfo[@"msadProvinceName"];
        _cityId = _addressInfo[@"msadCityId"];
        _cityName = _addressInfo[@"msadCityName"];
        _sectionId = _addressInfo[@"msadAreaId"];
        _sectionName = _addressInfo[@"msadAreaName"];
        _recivername.text = _addressInfo[@"msadReceiverName"];
        _phoneField.text = _addressInfo[@"msadMobileNo"];
        _detailAddress.text = [NSString stringWithFormat:@"%@",_addressInfo[@"msadAddr"]];
        _addressSection.text = [NSString stringWithFormat:@"%@%@%@",_addressInfo[@"msadProvinceName"],_addressInfo[@"msadCityName"],_addressInfo[@"msadAreaName"]];
        NSString * defaultAddress = [NSString stringWithFormat:@"%@",_addressInfo[@"msadIsDefault"]];
        if ([defaultAddress isEqualToString:@"1"]) {
            _scroButton.ifSelected = YES;
        }else{
            _scroButton.ifSelected = NO;
        }
    }else{//添加
         [_cancleButton setTitle:@"取消" forState:UIControlStateNormal];
    }
    
}

- (IBAction)back:(id)sender {
    [self backBtn];

    
}

#pragma mark  == 选择地址
- (IBAction)choseAddress:(id)sender {
    [self hideKeyBoard];
    UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"IndentsStoryboard" bundle:nil];
        ChoseAddressMapViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"ChoseAddressMap"];
    page.choseAddres = ^(NSMutableDictionary * addressDic){
         _addressSection.text = [NSString stringWithFormat:@"%@   %@   %@",addressDic[@"province"],addressDic[@"city"],addressDic[@"district"]];
        _detailAddress.text = addressDic[@"address"];
        _captionName = addressDic[@"province"];
        _cityName = addressDic[@"city"];
        _sectionName = addressDic[@"district"];
        
        DbDao *dao = [DbDao sharedInstance];
        CityArea *capArea = [dao getCityArea:[NSString stringWithFormat:@"select * from sys_area where a_name = '%@'",_captionName]][0];
        _captionId = [NSString stringWithFormat:@"%d",capArea.a_id];
        
        CityArea *cityArea = [dao getCityArea:[NSString stringWithFormat:@"select * from sys_area where a_name = '%@'",_cityName]][0];
        _cityId = [NSString stringWithFormat:@"%d",cityArea.a_id];
        
        CityArea *sectionArea = [dao getCityArea:[NSString stringWithFormat:@"select * from sys_area where a_name = '%@'",_sectionName]][0];
        _sectionId = [NSString stringWithFormat:@"%d",sectionArea.a_id];

    };
        [[Common getNavigation]pushViewController:page];

}


#pragma mark == 保存
- (IBAction)savehandel:(id)sender {
    
    if (![Common getLoginStatus]) {
        [[AppDelegate sharedAppDelegate]ShowLoginPage];
        return ;
    }
    
    if ([Common dataIsNULL:_recivername.text]) {
        [CWHUDTips showTips:_recivername.placeholder];
        return;
    }
    if ([Common dataIsNULL:_phoneField.text]) {
        [CWHUDTips showTips:_phoneField.placeholder];
        return;
    }
    if ([Common dataIsNULL:_addressSection.text]) {
        [CWHUDTips showTips:_addressSection.placeholder];
        return;
    }
    if ([Common dataIsNULL:_detailAddress.text]) {
        [CWHUDTips showTips:_detailAddress.placeholder];
        return;
    }
    
    NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
    
    [dic setObject:_captionId forKey:@"msadProvinceId"];
    [dic setObject:_captionName forKey:@"msadProvinceName"];
    [dic setObject:_cityId forKey:@"msadCityId"];
    [dic setObject:_cityName forKey:@"msadCityName"];
    [dic setObject:_sectionId forKey:@"msadAreaId"];
    [dic setObject:_sectionName forKey:@"msadAreaName"];
    [dic setObject:_recivername.text forKey:@"msadReceiverName"];
    [dic setObject:_phoneField.text forKey:@"msadMobileNo"];
    [dic setObject:_detailAddress.text forKey:@"msadAddr"];
    [dic setObject:[Common getLoginInfo][@"userId"] forKey:@"museId"];
    [dic setObject:[Common getLoginInfo][@"userId"] forKey:@"msadMemberId"];
    [dic setObject:[Common getLoginInfo][Key_User_password] forKey:@"musePwd"];
    [dic setObject:[Common getLoginInfo][Key_User_onlineKey] forKey:@"museOnlineTag"];
    
    
    if (_scroButton.ifSelected == YES) {
        [dic setObject:@"1" forKey:@"msadIsDefault"];
    }else{
        [dic setObject:@"0" forKey:@"msadIsDefault"];
    }
    
    if (_addressInfo) {//编辑
        [dic setObject:_addressInfo[@"msadId"] forKey:@"msadId"];
        self.shareApi.delegate=self;
        [self.shareApi  requestOperationWithDic:dic withPath:HK_updateMallShippingAddress withHttpType:@"POST" withTag:HK_HK_updateMallShippingAddress];
    }else{//添加
    self.shareApi.delegate=self;
    [self.shareApi  requestOperationWithDic:dic withPath:HK_addMallShippingAddress withHttpType:@"POST" withTag:HK_HK_addMallShippingAddress];
    }
    
}

#pragma mark  ==取消  删除
- (IBAction)cancleHandel:(id)sender {
    if (_addressInfo) {//编辑 删除
        if (![Common getLoginStatus]) {
            [[AppDelegate sharedAppDelegate]ShowLoginPage];
            return ;
        }
        NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
        [dic setObject:_addressInfo[@"msadId"] forKey:@"ids"];
        [dic setObject:[Common getLoginInfo][@"userId"] forKey:@"museId"];
        [dic setObject:[Common getLoginInfo][@"userId"] forKey:@"msadMemberId"];
        [dic setObject:[Common getLoginInfo][Key_User_password] forKey:@"musePwd"];
        [dic setObject:[Common getLoginInfo][Key_User_onlineKey] forKey:@"museOnlineTag"];
        self.shareApi.delegate=self;
        [self.shareApi  requestOperationWithDic:dic withPath:HK_deleteMallShippingAddress withHttpType:@"POST" withTag:HK_HK_deleteMallShippingAddress];
    }else{//取消添加
        [self backBtn];
    }
    
}


#pragma   mark---- GetADBanner Request Delegate

-(void)fetchDatabaseFinished:(NSMutableDictionary *)database withTag:(NSInteger)tag{
    
    if (tag==HK_HK_addMallShippingAddress) {
        
        if (  [[database valueForKey:@"flag"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else  {
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"] ];
            [self backBtn];
            
        }
        
    }else if (tag == HK_HK_updateMallShippingAddress){
        if (  [[database valueForKey:@"flag"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else  {
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  ];
            _endEditBlock(@"0");
            [self backBtn];
            
            
        }
        
    }else if (tag == HK_HK_deleteMallShippingAddress){
        if (  [[database valueForKey:@"flag"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else  {
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  ];
            _endEditBlock(@"1");
            [self backBtn];
            
        }
    }
}

-(void)fetchDatabaseFailed:(NSError *)error message:(NSString *)message{
    
    //
    [self dismissWaitDialog];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
